package daytwo.dowhile循环;

import java.util.Scanner;

/*
声明变量balance并初始化为0，用以表示银行账户的余额，下面通过ATM机程序实现存款，取款等功能。
=========ATM========
 1、存款
  2、取款
  3、显示余额
  4、退出
请选择(1-4)：
 */
public class pra2 {
    public static void main(String[] args) {
        int balance = 0;
        Scanner sc = new Scanner(System.in);
        boolean flag = true;
        do {
            System.out.println("=========ATM========");
            System.out.println("1、存款");
            System.out.println("2、取款");
            System.out.println("3、显示余额");
            System.out.println("4、退出");
            System.out.println("请选择1-4");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("请输入你要存入的金额");
                    int money = sc.nextInt();
                    balance = balance + money;
                    System.out.println("存款成功");
                    break;
                case 2:
                    System.out.println("请输入你要取出的金额");
                    int money2 = sc.nextInt();
                    if (balance >= money2) {
                        balance = balance - money2;
                        System.out.println("取款成功");
                    } else if (balance < money2) {
                        System.out.println("取款失败，余额不足");
                    }
                    break;
                case 3:
                    System.out.println("您的余额是"+balance);
                    break;
                    case 4:
                        System.out.println("您确定要退出吗");
                        Scanner sc1 = new Scanner(System.in);
                        char ch = sc1.next().charAt(0);
                        if (ch == 'Y') {
                            flag = false;
                        } else if (ch == 'N') {
                            flag = true;
                        }
                        break;
                default:
                    System.out.println("没有与之匹配的选项");
            }
        } while (flag);
    }
}
